﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using OpenCvSharp;
using OpenCvSharp.WpfExtensions;
using PaddleOCRSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using TlToolBase;
using TlToolCordCreate.Mode;

namespace TlToolCordCreate.ViewMode
{
    /// <summary>
    /// 类创建
    /// </summary>
    public class ClassCreate : ObservableObject
    {
        /// <summary>
        /// 配置
        /// </summary>
        public ClassCreateConfig Config { get => _config; set => SetProperty(ref _config, value); }
        private ClassCreateConfig _config;

        private string _field;
        /// <summary>
        /// 字段
        /// </summary>
        public string Field { get => _field; set => SetProperty(ref _field, value); }


        public ICommand SaveCodeCommand { get; set; }
        /// <summary>
        /// 加载文件
        /// </summary>
        public ICommand LoadFileCommand { get; set; }


        public ClassCreate()
        {
            SaveCodeCommand = new RelayCommand(SaveCode);
            LoadFileCommand = new RelayCommand(LoadFile);
            Config = FileDataClass.Get<ClassCreateConfig>();
        }

        private void LoadFile()
        {
            Console.Clear();
            string str = "";
            var paths = FileHelp.LoadImg("BitmapFile");
            for (int i = 0; i < paths.Result.Count; i++)
            {
                OCRUtils ocr = new OCRUtils();
                Bitmap bit3 = new Bitmap(paths.Result[i]);
                str += ocr.BitmapToString(bit3);
            }
            Console.WriteLine(str);
            Field = str;
        }
        /// <summary>
        /// 保存代码
        /// </summary>
        private void SaveCode()
        {
            if (string.IsNullOrEmpty(Field))
            {
                return;
            }
            Config=FileDataClass.Get<ClassCreateConfig>();
            string classfile = "";
            //classfile += CreateLine("namespace {0};", Config.NameSpan);
            //classfile += CreateLine("public class {0}", Config.ClassName);
            //classfile += CreateLine("{");


            var fieds = Field.Split(" ");
            int create = 0;
            for (int i = 0; i < fieds.Length; i++)
            {
                var value = Config.Data.Find(x => x.Key == fieds[i]);
                if (value == null)
                {
                    Config.Data.Add(new NameAndType()
                    {
                        Name = "",
                        Key = fieds[i],
                        Type = ""
                    });
                    FileDataClass.Save(Config);
                    Console.WriteLine("生成失败缺失字段" + fieds[i]);
                }
                else
                {
                    classfile += CreateLine("   /// <summary>");
                    classfile += CreateLine("   /// {0}", fieds[i]);
                    classfile += CreateLine("   /// </summary>", fieds[i]);
                    classfile += CreateLine("   public " + value.Type + " " + value.Name + " { get; set; }");
                    create++;
                }
             
            }
            Console.WriteLine("解析字段数量" + fieds.Length +"实际生成字段数量"+ create);
            //classfile += CreateLine("}");
            FileSave(Config.ClassName+".cs", "Class", classfile);
        }
        private void FileSave(string filename, string path, string value)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (File.Exists(path + "\\" + filename))
            {
                File.Delete(path + "\\" + filename);
            }

            File.Create(path+"\\"+filename).Close();
            File.WriteAllText(path + "\\" + filename, value);
        }
        private string CreateLine(string line, params object[] strings)
        {
            if (strings == null || strings.Length == 0)
            {
                return line + "\r\n";
            }
            return string.Format(line + "\r\n", strings);
        }
    }
}
